package com.jp.knowledge.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMEmoji;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMediaObject;
import com.umeng.socialize.media.UMusic;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/7/5.
 */

public class ShareBuilder implements ShareBoardlistener, UMShareListener {

	private Activity activity;
	private ShareAction shareAction;
	private boolean withSINA;
	private boolean withQQ;
	private boolean withQZONE;
	private boolean withWEIXIN;
	private boolean withWEIXIN_CIRCLE;
	private boolean withCOPY;
	private boolean withQR_CODE;
	private UMShareAPI umShareAPI;
	private ClipboardManager clipboardManager;
	private List<ShareBtnBean> shareBtnList;

	private String mCopyContent;
	private String mQrCodeUrl;

	private Dialog qrCodeDialog;
	private ImageView qrCodeImg;

	public ShareBuilder(Activity activity){
		this.activity = activity;
		shareAction = new ShareAction(activity);
		withSINA = true;
		withQQ = true;
		withQZONE = true;
		withWEIXIN = true;
		withWEIXIN_CIRCLE = true;
		withQR_CODE = true;
		withCOPY = true;
		shareBtnList = new ArrayList<>();
		clipboardManager = (ClipboardManager)activity.getSystemService(Context.CLIPBOARD_SERVICE);
		umShareAPI = UMShareAPI.get(activity);
		shareAction.setShareboardclickCallback(this);
		shareAction.setCallback(this);

		qrCodeDialog = new Dialog(this.activity, R.style.custom_idalgo);
		qrCodeImg = new ImageView(this.activity);
		qrCodeImg.setBackgroundColor(activity.getResources().getColor(R.color.white));
		qrCodeImg.setScaleType(ImageView.ScaleType.FIT_CENTER);
		qrCodeDialog.setContentView(qrCodeImg);
		qrCodeImg.getLayoutParams().width = (int) (this.activity.getResources().getDisplayMetrics().widthPixels*0.8);
		qrCodeImg.getLayoutParams().height = qrCodeImg.getLayoutParams().width;
	}

	public ShareBuilder withSINA(boolean withSINA){
		this.withSINA = withSINA;
		return this;
	}

	public ShareBuilder withQQ(boolean withQQ){
		this.withQQ = withQQ;
		return this;
	}

	public ShareBuilder withQZONE(boolean withQZONE){
		this.withQZONE = withQZONE;
		return this;
	}

	public ShareBuilder withWEIXIN(boolean withWEIXIN){
		this.withWEIXIN = withWEIXIN;
		return this;
	}

	public ShareBuilder withWEIXIN_CIRCLE(boolean withWEIXIN_CIRCLE){
		this.withWEIXIN_CIRCLE = withWEIXIN_CIRCLE;
		return this;
	}

	public ShareBuilder withCOPY(boolean withCOPY){
		this.withCOPY = withCOPY;
		return this;
	}

	public ShareBuilder withQR_CODE(boolean withQR_CODE){
		this.withQR_CODE = withQR_CODE;
		return this;
	}

	public ShareBuilder withCopyContent(String copyContent){
		this.mCopyContent = copyContent;
		return this;
	}

	public ShareBuilder withQRCodeUrl(String qrCodeUrl){
		this.mQrCodeUrl = qrCodeUrl;
		return this;
	}

	public ShareBuilder withTitle(String title){
		shareAction.withTitle(title);
		return this;
	}

	public ShareBuilder withText(String text){
		shareAction.withText(text);
		return this;
	}

	public ShareBuilder withTargetUrl(String url){
		shareAction.withTargetUrl(url);
		return this;
	}

	public ShareBuilder withExtra(UMImage mExtra){
		shareAction.withExtra(mExtra);
		return this;
	}

	public ShareBuilder withFile(File file){
		shareAction.withFile(file);
		return this;
	}

	public ShareBuilder withFollow(String follow){
		shareAction.withFollow(follow);
		return this;
	}

	public ShareBuilder withMedia(UMImage image){
		shareAction.withMedia(image);
		return this;
	}

	public ShareBuilder withMedia(UMEmoji emoji){
		shareAction.withMedia(emoji);
		return this;
	}

	public ShareBuilder withMedia(UMusic music){
		shareAction.withMedia(music);
		return this;
	}

	public ShareBuilder withMedia(UMVideo video){
		shareAction.withMedia(video);
		return this;
	}

	public ShareBuilder withShareBoardDirection(View view, int gravity){
		shareAction.withShareBoardDirection(view, gravity);
		return this;
	}

	public ShareBuilder addButton(String showword, String icon){
		return addButton(showword, icon, null);
	}

	public ShareBuilder addButton(String showword, String icon, ShareIconClickListener iconClickListener){
		return addButton(showword, showword, icon, icon, iconClickListener);
	}

	public ShareBuilder addButton(String showword, String keyword, String icon, String grayicon) {
		return addButton(showword, keyword, icon, grayicon, null);
	}

	public  ShareBuilder addButton(String showword, String keyword, String icon, String grayicon, ShareIconClickListener iconClickListener) {
		shareBtnList.add(new ShareBtnBean(showword, keyword, icon, grayicon, iconClickListener));
		return this;
	}

	public boolean isInstall(SHARE_MEDIA shareMedia){
		return umShareAPI.isInstall(activity, shareMedia);
	}

	public boolean isSupport(SHARE_MEDIA shareMedia){
		return umShareAPI.isSupport(activity, shareMedia);
	}

	public String getVersion(SHARE_MEDIA shareMedia){
		return umShareAPI.getversion(activity, shareMedia);
	}

	public ShareAction build(){
		List<SHARE_MEDIA> shareMediaList = new ArrayList<>();
		if(withWEIXIN)
			shareMediaList.add(SHARE_MEDIA.WEIXIN);
		if(withWEIXIN_CIRCLE)
			shareMediaList.add(SHARE_MEDIA.WEIXIN_CIRCLE);
		if(withQQ)
			shareMediaList.add(SHARE_MEDIA.QQ);
		if(withQZONE)
			shareMediaList.add(SHARE_MEDIA.QZONE);
		if(withSINA)
			shareMediaList.add(SHARE_MEDIA.SINA);
		if(shareMediaList.size() > 0){
			SHARE_MEDIA[] shareMedias = new SHARE_MEDIA[shareMediaList.size()];
			for(int i = 0, j = shareMediaList.size(); i < j; i++)
				shareMedias[i] = shareMediaList.get(i);
			shareAction.setDisplayList(shareMedias);
		}
		if(withCOPY)
			shareAction.addButton("share_copy_url",
					"share_copy_url",
					"ttoutiaofuzhilianjie",
					"ttoutiaofuzhilianjie");
		if(withQR_CODE)
			shareAction.addButton("share_qr_code",
					"share_qr_code",
					"qrcodeshareit",
					"qrcodeshareit");
		for(ShareBtnBean shareBtnBean : shareBtnList)
			shareAction.addButton(shareBtnBean.getShowword(),
					shareBtnBean.getKeyword(),
					shareBtnBean.getIcon(),
					shareBtnBean.getGrayicon());
		return shareAction;
	}

	@Override
	public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
		if("share_copy_url".equals(snsPlatform.mShowWord)){
			if(mCopyContent != null) {
				clipboardManager.setText(mCopyContent);
				ToasUtil.toast(activity, "复制成功");
			} else{
				ToasUtil.toast(activity, "无内容可复制！");
			}
			return;
		} else if("share_qr_code".equals(snsPlatform.mShowWord)){
			showQrCode(mQrCodeUrl);
			return;
		}
		for(ShareBtnBean shareBtnBean : shareBtnList){
			if(shareBtnBean.getKeyword() == null || !shareBtnBean.getKeyword().equals(snsPlatform.mShowWord))
				continue;
			if(shareBtnBean.getIconClickListener() != null)
				shareBtnBean.getIconClickListener().onShareIconClick(snsPlatform, share_media);
			return;
		}
		shareAction.setPlatform(share_media);
		shareAction.share();
	}

	@Override
	public void onResult(SHARE_MEDIA share_media) {
		if(onSuccessListener != null)
			onSuccessListener.onSuccess(share_media);
		else
			ToasUtil.toast(activity, "分享成功");
	}

	@Override
	public void onError(SHARE_MEDIA share_media, Throwable throwable) {
		if(onErrorListener != null)
			onErrorListener.onError(share_media, throwable);
		else
			ToasUtil.toast(activity, "分享失败");
	}

	@Override
	public void onCancel(SHARE_MEDIA share_media) {
		if(onCancelListener != null)
			onCancelListener.onCancel(share_media);
		else
			ToasUtil.toast(activity, "分享取消");
	}

	private void showQrCode(final String url){
		QRCodeUtils.encodeAsBitmap(url, new QRCodeUtils.EncodeListener() {
			@Override
			public void encodeSuccedd(Bitmap qrCode) {
				qrCodeImg.setImageBitmap(qrCode);
				qrCodeDialog.show();
			}
			@Override
			public void encodeError(Throwable e) {
				qrCodeImg.setImageResource(R.mipmap.morentutouxiang);
				qrCodeDialog.show();
			}
		});
	}

	private ShareSuccessListener onSuccessListener;
	private ShareErrorListener onErrorListener;
	private ShareCancelListener onCancelListener;
	public void withSuccessListener(ShareSuccessListener onSuccessListener) {
		this.onSuccessListener = onSuccessListener;
	}
	public void withErrorListener(ShareErrorListener onErrorListener) {
		this.onErrorListener = onErrorListener;
	}
	public void withCancelListener(ShareCancelListener onCancelListener) {
		this.onCancelListener = onCancelListener;
	}
	public interface ShareIconClickListener{
		void onShareIconClick(SnsPlatform snsPlatform, SHARE_MEDIA share_media);
	}
	public interface ShareSuccessListener{
		void onSuccess(SHARE_MEDIA share_media);
	}
	public interface ShareErrorListener{
		void onError(SHARE_MEDIA share_media, Throwable throwable);
	}
	public interface ShareCancelListener{
		void onCancel(SHARE_MEDIA share_media);
	}

	private class ShareBtnBean{
		private String showword;
		private String keyword;
		private String icon;
		private String grayicon;
		private ShareIconClickListener iconClickListener;
		public ShareBtnBean(){}
		public ShareBtnBean(String showword, String keyword, String icon, String grayicon, ShareIconClickListener iconClickListener){
			setShowword(showword);
			setKeyword(keyword);
			setIcon(icon);
			setGrayicon(grayicon);
			setIconClickListener(iconClickListener);
		}
		public String getShowword() {
			return showword;
		}
		public void setShowword(String showword) {
			this.showword = showword;
		}
		public String getKeyword() {
			return keyword;
		}
		public void setKeyword(String keyword) {
			this.keyword = keyword;
		}
		public String getIcon() {
			return icon;
		}
		public void setIcon(String icon) {
			this.icon = icon;
		}
		public String getGrayicon() {
			return grayicon;
		}
		public void setGrayicon(String grayicon) {
			this.grayicon = grayicon;
		}
		public ShareIconClickListener getIconClickListener() {
			return iconClickListener;
		}
		public void setIconClickListener(ShareIconClickListener iconClickListener) {
			this.iconClickListener = iconClickListener;
		}
	}
}
